/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.org.pslava.codingtest.model;

import java.util.HashMap;

/**
 * Encapsulates a functionality of basket. <br/>
 *
 * 
 * @author Vyacheslav Panasyuk.
 */
public class Basket implements IBasket {

    private HashMap<String, BasketItem> basket;

    public Basket() {
        this.basket = new HashMap<String, BasketItem>();
    }

    @Override
    public void add(BasketItem item) {
        if (basket.containsKey(item.getProductName())) {
            basket.get(item.getProductName()).add();
        } else {
            basket.put(item.getProductName(), item);
        }
    }

    @Override
    public void delete(BasketItem item) {
        String itemName = item.getProductName();
        if (basket.containsKey(itemName)) {
            basket.get(itemName).delete();
            if (basket.get(itemName).getCount() == 0) {
                basket.remove(itemName);
            }
        }
    }

    public Double calculate() {
        double result = 0;
        for (BasketItem basketItem : basket.values()) {
            result += basketItem.calculate();
        }

        return result;
    }
}
